#include "configurationmodel.h"

#include <QSettings>

ConfigurationManager::ConfigurationManager(QString appName)
{
	m_appName = appName;
	
	defaultFontColor = QColor(Qt::black);
	editorColor = QColor(Qt::white);
	treeColor = QColor(Qt::white);
	
	readSettings();
}

ConfigurationManager::~ConfigurationManager()
{
	writeSettings();
}

void ConfigurationManager::readSettings()
{
	#ifdef Q_WS_WIN
	QSettings settings(m_appName + ".ini", QSettings::IniFormat, 0);
	#else
	QSettings settings(QSettings::IniFormat, QSettings::UserScope, m_appName, m_appName, 0);
	#endif

    settings.beginGroup("Configuration");
    defaultFont = settings.value("DefaultFont", QString("Arial")).toString();
    defaultFontSize = settings.value("DefaultFontSize", QString("9")).toString();
    defaultFontColor = settings.value("DefaultFontColor", QColor(Qt::black)).value<QColor>();
    editorColor = settings.value("EditorColor", QColor(Qt::white)).value<QColor>();
    treeColor = settings.value("TreeColor", QColor(Qt::white)).value<QColor>();
    useTrayIcon = settings.value("UseTrayIcon", QVariant(false).toString()).toBool();
    closeToTray = settings.value("CloseToTray", QVariant(false).toString()).toBool();
    showMessageOnCloseToTray = settings.value("ShowMessageOnCloseToTray", QVariant(true).toString()).toBool();
    openLastFile = settings.value("OpenLastFile", QVariant(false).toString()).toBool();
    lastFile = settings.value("LastFile", QString("")).toString();
    settings.endGroup();
}

void ConfigurationManager::writeSettings()
{
	#ifdef Q_WS_WIN
	QSettings settings(m_appName + ".ini", QSettings::IniFormat, 0);
	#else
	QSettings settings(QSettings::IniFormat, QSettings::UserScope, m_appName, m_appName, 0);
	#endif

    settings.beginGroup("Configuration");
    settings.setValue("DefaultFont", defaultFont);
    settings.setValue("DefaultFontSize", defaultFontSize);
    settings.setValue("DefaultFontColor", defaultFontColor);
    settings.setValue("EditorColor", editorColor);
    settings.setValue("TreeColor", treeColor);
    settings.setValue("UseTrayIcon", useTrayIcon);
    settings.setValue("CloseToTray", closeToTray);
    settings.setValue("ShowMessageOnCloseToTray", showMessageOnCloseToTray);
    settings.setValue("OpenLastFile", openLastFile);
    settings.setValue("LastFile", lastFile);
    settings.endGroup();
}
